﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;

namespace Sinacor.OMM.Deployment.Setup.Steps
{
    public class UpdateConfig
    {
        #region [ Public Methods ]

        public static void UpdateConfigs(string companyId)
        {
            IEnumerable<string> files = Directory.GetFiles(Environment.CurrentDirectory, "*.config", SearchOption.AllDirectories);
                        
            foreach (string file in files)
            {
                if (file.Contains("OMM") || file.Contains("OrderMessageManager"))
                {
                    XmlDocument document = new XmlDocument();
                    document.Load(file);

                    XmlNodeList nodeList = document.SelectNodes("/configuration/appSettings/add");

                    foreach (XmlNode node in nodeList)
                    {
                        if (node.Attributes[0].Value.ToUpper() == "COMPANYID")
                        {
                            node.Attributes[1].Value = companyId;
                            document.Save(file);
                            break;
                        }
                    }
                }
            }
        }

        #endregion
    }
}
